﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Reflection;

namespace SoftballStats
{
    public partial class TeamPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Season"] != null)
            {
                SoftballStats_BL.Season thisSeason = new SoftballStats_BL.Season();
                thisSeason = Session["Season"] as SoftballStats_BL.Season;
                lblCurrentSeason.Text = thisSeason.Description + " " + thisSeason.Year;
                SoftballStats_BL.Team myTeam = new SoftballStats_BL.Team();

                if (Request.QueryString["TeamID"] != null)
                {
                    SoftballStats_BL.Team selectedTeam = SoftballStats_BL.Team.GetTeamByID(Guid.Parse(Request.QueryString["TeamID"]));
                    if (selectedTeam.ID != Guid.Empty)
                    {
                        Session["Team"] = selectedTeam;

                        /* remove TeamID from QueryString */
                        PropertyInfo isReadOnly = typeof(NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
                        isReadOnly.SetValue(this.Request.QueryString, false, null);
                        this.Request.QueryString.Remove("TeamID");
                        isReadOnly.SetValue(this.Request.QueryString, true, null);
                    }
                }
                if (Session["Team"] != null)
                {
                    /* TeamInfo tab */
                    pnlTeamInfo.Visible = true;
                    myTeam = Session["Team"] as SoftballStats_BL.Team;
                    imgLogo.ImageUrl = "Images/" + myTeam.Name + ".jpg";
                    lblName.Text = myTeam.Name;
                    lblHeadCoach.Text = myTeam.HeadCoach;
                    lblSeason.Text = thisSeason.Description + " " + thisSeason.Year;

                    GetRecord(myTeam.ID);

                    /* Roster tab */
                    GetRoster(myTeam);

                    /* Schedule tab */
                    GetSchedule(myTeam);

                    /* Stats tab */
                    GetStats(myTeam, thisSeason);
                }
                else
                {
                    pnlTeamInfo.Visible = false;
                }
            }
            else
            {
                Response.Redirect("../SelectSeason.aspx");
            }

            GetTeams();
        }

        private void GetStats(SoftballStats_BL.Team myTeam, SoftballStats_BL.Season thisSeason)
        {
            List<SoftballStats_BL.Player> playersOnTeam = SoftballStats_BL.Player.GetPlayersByTeamID(myTeam.ID);
            List<SoftballStats_BL.PlayerGame> playerSeasonStatsList = new List<SoftballStats_BL.PlayerGame>();

            for (int i = 0; i < playersOnTeam.Count; i++)
            {
                SoftballStats_BL.PlayerGame playerSeasonStats = SoftballStats_BL.PlayerGame.GetPlayerSeasonStats(playersOnTeam[i].ID, "", "", thisSeason.ID);
                playerSeasonStatsList.Add(playerSeasonStats);
            }

            Guid averageLeaderID = new Guid();
            Guid homeRunsLeaderID = new Guid();
            Guid rbiLeaderID = new Guid();
            Guid obpLeaderID = new Guid();
            Guid hitsLeaderID = new Guid();
            string averageLeader = "";
            string homeRunsLeader = "";
            string rbiLeader = "";
            string obpLeader = "";
            string hitsLeader = "";

            for (int i = 0; i < playerSeasonStatsList.Count; i++)
            {
                if (averageLeaderID == Guid.Empty || (double.Parse(playerSeasonStatsList[i].Average) >= double.Parse(averageLeader)))
                {
                    averageLeaderID = playerSeasonStatsList[i].PlayerID;
                    if (averageLeader == string.Empty || (double.Parse(playerSeasonStatsList[i].Average) > double.Parse(averageLeader)))
                    {
                        averageLeader = playerSeasonStatsList[i].Average;
                        lblAverageTie.Visible = false;
                    }
                    else
                    {
                        averageLeader = playerSeasonStatsList[i].Average;
                        lblAverageTie.Visible = true;
                    }
                }
                if (homeRunsLeaderID == Guid.Empty || (playerSeasonStatsList[i].HomeRuns >= short.Parse(homeRunsLeader)))
                {
                    homeRunsLeaderID = playerSeasonStatsList[i].PlayerID;
                    if (homeRunsLeader == string.Empty || (playerSeasonStatsList[i].HomeRuns > short.Parse(homeRunsLeader)))
                    {
                        homeRunsLeader = playerSeasonStatsList[i].HomeRuns.ToString();
                        lblHRTie.Visible = false;
                    }
                    else
                    {
                        homeRunsLeader = playerSeasonStatsList[i].HomeRuns.ToString();
                        lblHRTie.Visible = true;
                    }
                }
                if (rbiLeaderID == Guid.Empty || (playerSeasonStatsList[i].RunsBattedIn >= short.Parse(rbiLeader)))
                {
                    rbiLeaderID = playerSeasonStatsList[i].PlayerID;
                    if (rbiLeader == string.Empty || (playerSeasonStatsList[i].RunsBattedIn > short.Parse(rbiLeader)))
                    {
                        rbiLeader = playerSeasonStatsList[i].RunsBattedIn.ToString();
                        lblRBITie.Visible = false;
                    }
                    else
                    {
                        rbiLeader = playerSeasonStatsList[i].RunsBattedIn.ToString();
                        lblRBITie.Visible = true;
                    }
                }
                if (obpLeaderID == Guid.Empty || (double.Parse(playerSeasonStatsList[i].OnBasePercentage) >= double.Parse(obpLeader)))
                {
                    obpLeaderID = playerSeasonStatsList[i].PlayerID;
                    if (obpLeader == string.Empty || (double.Parse(playerSeasonStatsList[i].OnBasePercentage) > double.Parse(obpLeader)))
                    {
                        obpLeader = playerSeasonStatsList[i].OnBasePercentage;
                        lblOBPTie.Visible = false;
                    }
                    else
                    {
                        obpLeader = playerSeasonStatsList[i].OnBasePercentage;
                        lblOBPTie.Visible = true;
                    }
                }
                if (hitsLeaderID == Guid.Empty || (playerSeasonStatsList[i].Hits >= short.Parse(hitsLeader)))
                {
                    hitsLeaderID = playerSeasonStatsList[i].PlayerID;
                    if (hitsLeader == string.Empty || (playerSeasonStatsList[i].Hits > short.Parse(hitsLeader)))
                    {
                        hitsLeader = playerSeasonStatsList[i].Hits.ToString();
                        lblHitsTie.Visible = false;
                    }
                    else
                    {
                        hitsLeader = playerSeasonStatsList[i].Hits.ToString();
                        lblHitsTie.Visible = true;
                    }
                }
            }

            //Average
            SoftballStats_BL.Player averagePlayer = SoftballStats_BL.Player.GetPlayerByID(averageLeaderID);
            lblAverageLeaderName.Text = averagePlayer.FirstName + " " + averagePlayer.LastName;
            lblAverageLeaderNumber.Text = "#" + averagePlayer.Number;
            lblAverageLeaderPosition.Text = averagePlayer.Position;
            lblAverageLeaderStat.Text = averageLeader;
            imgAverageLeader.ImageUrl = SoftballStats_BL.Player.GetPlayerImage(lblAverageLeaderName.Text);

            //Home Runs
            SoftballStats_BL.Player homeRunsPlayer = SoftballStats_BL.Player.GetPlayerByID(homeRunsLeaderID);
            lblHRLeaderName.Text = homeRunsPlayer.FirstName + " " + homeRunsPlayer.LastName;
            lblHRLeaderNumber.Text = "#" + homeRunsPlayer.Number;
            lblHRLeaderPosition.Text = homeRunsPlayer.Position;
            lblHRLeaderStat.Text = homeRunsLeader;
            imgHRLeader.ImageUrl = SoftballStats_BL.Player.GetPlayerImage(lblHRLeaderName.Text);

            //RBI
            SoftballStats_BL.Player rbiPlayer = SoftballStats_BL.Player.GetPlayerByID(rbiLeaderID);
            lblRBILeaderName.Text = rbiPlayer.FirstName + " " + rbiPlayer.LastName;
            lblRBILeaderNumber.Text = "#" + rbiPlayer.Number;
            lblRBILeaderPosition.Text = rbiPlayer.Position;
            lblRBILeaderStat.Text = rbiLeader;
            imgRBILeader.ImageUrl = SoftballStats_BL.Player.GetPlayerImage(lblRBILeaderName.Text);

            //OBP
            SoftballStats_BL.Player obpPlayer = SoftballStats_BL.Player.GetPlayerByID(obpLeaderID);
            lblOBPLeaderName.Text = obpPlayer.FirstName + " " + obpPlayer.LastName;
            lblOBPLeaderNumber.Text = "#" + obpPlayer.Number;
            lblOBPLeaderPosition.Text = obpPlayer.Position;
            lblOBPLeaderStat.Text = obpLeader;
            imgOBPLeader.ImageUrl = SoftballStats_BL.Player.GetPlayerImage(lblOBPLeaderName.Text);

            //Hits
            SoftballStats_BL.Player hitsPlayer = SoftballStats_BL.Player.GetPlayerByID(hitsLeaderID);
            lblHitsLeaderName.Text = hitsPlayer.FirstName + " " + hitsPlayer.LastName;
            lblHitsLeaderNumber.Text = "#" + hitsPlayer.Number;
            lblHitsLeaderPosition.Text = hitsPlayer.Position;
            lblHitsLeaderStat.Text = hitsLeader;
            imgHitsLeader.ImageUrl = SoftballStats_BL.Player.GetPlayerImage(lblHitsLeaderName.Text);
        }

        private void GetSchedule(SoftballStats_BL.Team myTeam)
        {
            List<SoftballStats_BL.Game> lstGames = SoftballStats_BL.Game.GetGamesByTeamID(myTeam.ID);

            for (int i = 0; i < lstGames.Count; i++)
            {
                HtmlTableRow tblRow = new HtmlTableRow();

                HtmlTableCell tblCell = new HtmlTableCell();
                LinkButton lnkDate = new LinkButton();
                lnkDate.Text = lstGames[i].Date.ToString();
                lnkDate.PostBackUrl = "~/GamePage.aspx?GameID=" + lstGames[i].ID.ToString();
                tblCell.Controls.Add(lnkDate);
                tblRow.Cells.Add(tblCell);

                if (lstGames[i].IsHome)
                {
                    HtmlTableCell tblCell2 = new HtmlTableCell();
                    Label lblVisitor = new Label();
                    lblVisitor.Text = lstGames[i].OpponentName;
                    tblCell2.Controls.Add(lblVisitor);
                    tblRow.Cells.Add(tblCell2);

                    HtmlTableCell tblCell3 = new HtmlTableCell();
                    Label lblVScore = new Label();
                    lblVScore.Text = lstGames[i].OpponentScore.ToString();
                    tblCell3.Controls.Add(lblVScore);
                    tblRow.Cells.Add(tblCell3);

                    HtmlTableCell tblCell4 = new HtmlTableCell();
                    Label lblHome = new Label();
                    lblHome.Text = myTeam.Name;
                    tblCell4.Controls.Add(lblHome);
                    tblRow.Cells.Add(tblCell4);

                    HtmlTableCell tblCell5 = new HtmlTableCell();
                    Label lblHScore = new Label();
                    lblHScore.Text = lstGames[i].TeamScore.ToString();
                    tblCell5.Controls.Add(lblHScore);
                    tblRow.Cells.Add(tblCell5);
                }
                else
                {
                    HtmlTableCell tblCell2 = new HtmlTableCell();
                    Label lblVisitor = new Label();
                    lblVisitor.Text = myTeam.Name;
                    tblCell2.Controls.Add(lblVisitor);
                    tblRow.Cells.Add(tblCell2);

                    HtmlTableCell tblCell3 = new HtmlTableCell();
                    Label lblVScore = new Label();
                    lblVScore.Text = lstGames[i].TeamScore.ToString();
                    tblCell3.Controls.Add(lblVScore);
                    tblRow.Cells.Add(tblCell3);

                    HtmlTableCell tblCell4 = new HtmlTableCell();
                    Label lblHome = new Label();
                    lblHome.Text = lstGames[i].OpponentName;
                    tblCell4.Controls.Add(lblHome);
                    tblRow.Cells.Add(tblCell4);

                    HtmlTableCell tblCell5 = new HtmlTableCell();
                    Label lblHScore = new Label();
                    lblHScore.Text = lstGames[i].OpponentScore.ToString();
                    tblCell5.Controls.Add(lblHScore);
                    tblRow.Cells.Add(tblCell5);
                }

                HtmlTableCell tblCell6 = new HtmlTableCell();
                Label lblOutcome = new Label();
                if (lstGames[i].IsWin)
                {
                    lblOutcome.Text = "W";
                    tblCell6.BgColor = "Green";
                    lblOutcome.ForeColor = System.Drawing.Color.White;
                }
                else
                {
                    lblOutcome.Text = "L";
                    tblCell6.BgColor = "Red";
                    lblOutcome.ForeColor = System.Drawing.Color.White;
                }
                tblCell6.Controls.Add(lblOutcome);
                tblCell6.Align = "center";
                tblRow.Cells.Add(tblCell6);

                HtmlTableCell tblCell7 = new HtmlTableCell();
                Label lblLocation = new Label();
                lblLocation.Text = lstGames[i].Location;
                tblCell7.Controls.Add(lblLocation);
                tblRow.Cells.Add(tblCell7);

                tblSchedule.Rows.Add(tblRow);
            }
        }

        private void GetRoster(SoftballStats_BL.Team myTeam)
        {
            List<SoftballStats_BL.Player> lstPlayers = SoftballStats_BL.Player.GetPlayersByTeamID(myTeam.ID);

            for (int i = 0; i < lstPlayers.Count; i++)
            {
                HtmlTableRow tblRow = new HtmlTableRow();

                HtmlTableCell tblCell = new HtmlTableCell();
                Label lblNumber = new Label();
                lblNumber.Text = lstPlayers[i].Number.ToString();
                tblCell.Controls.Add(lblNumber);
                tblRow.Cells.Add(tblCell);
                
                HtmlTableCell tblCell2 = new HtmlTableCell();
                LinkButton lnkName = new LinkButton();
                lnkName.Text = lstPlayers[i].FirstName + " " + lstPlayers[i].LastName;
                lnkName.PostBackUrl = "~/PlayerPage.aspx?PlayerID=" + lstPlayers[i].ID.ToString();
                tblCell2.Controls.Add(lnkName);
                tblRow.Cells.Add(tblCell2);

                HtmlTableCell tblCell3 = new HtmlTableCell();
                Label lblPosition = new Label();
                lblPosition.Text = lstPlayers[i].Position;
                tblCell3.Controls.Add(lblPosition);
                tblRow.Cells.Add(tblCell3);

                HtmlTableCell tblCell4 = new HtmlTableCell();
                Label lblBats = new Label();
                lblBats.Text = lstPlayers[i].Bats.ToString();
                tblCell4.Controls.Add(lblBats);
                tblRow.Cells.Add(tblCell4);

                HtmlTableCell tblCell5 = new HtmlTableCell();
                Label lblThrows = new Label();
                lblThrows.Text = lstPlayers[i].Throws.ToString();
                tblCell5.Controls.Add(lblThrows);
                tblRow.Cells.Add(tblCell5);

                tblPlayers.Rows.Add(tblRow);
            }
        }

        private void GetRecord(Guid teamID)
        {
            List<short> lstRecord = SoftballStats_BL.Team.GetRecordByTeamID(teamID);
            lblWins.Text = lstRecord[0].ToString();
            lblLosses.Text = lstRecord[1].ToString();
        }

        private void GetTeams()
        {
            SoftballStats_BL.Season thisSeason = Session["Season"] as SoftballStats_BL.Season;
            List<SoftballStats_BL.Team> lstTeams = SoftballStats_BL.Team.GetTeamsBySeasonID(thisSeason.ID);

            for (int i = 0; i < lstTeams.Count; i++)
            {
                HtmlTableRow tblRow = new HtmlTableRow();

                HtmlTableCell tblCell = new HtmlTableCell();
                LinkButton lnkSelect = new LinkButton();
                lnkSelect.Text = "Select";
                lnkSelect.PostBackUrl = "~/TeamPage.aspx?TeamID=" + lstTeams[i].ID.ToString();
                tblCell.Controls.Add(lnkSelect);
                tblRow.Cells.Add(tblCell);

                HtmlTableCell tblCell2 = new HtmlTableCell();
                Label lblName = new Label();
                lblName.Text = lstTeams[i].Name;
                tblCell2.Controls.Add(lblName);
                tblRow.Cells.Add(tblCell2);

                HtmlTableCell tblCell3 = new HtmlTableCell();
                Label lblHeadCoach = new Label();
                lblHeadCoach.Text = lstTeams[i].HeadCoach;
                tblCell3.Controls.Add(lblHeadCoach);
                tblRow.Cells.Add(tblCell3);

                HtmlTableCell tblCell4 = new HtmlTableCell();
                Label lblID = new Label();
                lblID.Text = lstTeams[i].ID.ToString();
                tblCell4.Controls.Add(lblID);
                tblRow.Cells.Add(tblCell4);
                tblRow.Cells[3].Style.Add("display", "none");

                tblTeams.Rows.Add(tblRow);
            }
        }
    }
}